package com.zhy.http.okhttp;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.gson.Gson;
import com.zhy.http.library.OkHttpUtils;
import com.zhy.http.library.callback.StringCallback;
import com.zhy.http.library.https.HttpsUtils;
import com.zhy.http.library.utils.L;
import com.zhy.http.okhttp.model.User;

import okhttp3.Call;
import okhttp3.OkHttpClient;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

public class GetTest {
    @Rule
    public OkhttpRule rule = new OkhttpRule();

    @Test
    public void testIsMax() {
        assertTrue(true);
    }

    @Test
    public void testGet() {
        String url = "https://api.github.com";
        OkHttpUtils.get()
                .url(url)
                .id(100)
                .tag("Get")
                .build()
                .execute(
                        new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                L.d("testGet: " + call.toString());
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                assertTrue(response.length() > 0);
                            }

                            @Override
                            public void onAfter(int id) {
                                super.onAfter(id);
                            }
                        });
    }

    @Test
    public void testGetUser() {
        String url = "https://api.github.com/users/wjwite";
        OkHttpUtils.get()
                .url(url)
                .id(100)
                .tag("GetUser")
                .build()
                .execute(
                        new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                L.d("testGetUser: " + call.toString());
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                Gson g = new Gson();
                                User user = g.fromJson(response, User.class);
                                assertEquals("wjWite", user.getLogin());
                                assertEquals("User", user.getType());
                            }
                        });
    }

    @After
    public void onFinishTag() {
        OkHttpUtils.getInstance().cancelTag("Get");
        OkHttpUtils.getInstance().cancelTag("GetUser");
    }
}
